// 页面跳转
function open(path, object = {}, closeSelf = 0) {
  if (!path) return false
  let datas = ''
  // 如果有两个参数且第二个为closeSelf，则直接跳转。如果第二个为参数对象，则展开
  if (arguments.length == 2 && typeof object == 'number') {
    closeSelf = object 
  } else if (object || Object.keys(object).length !== 0) {
    for (const key in object) {
      if (object.hasOwnProperty(key)) {
        datas += `${key}=${object[key]}&`
      } 
    }
    datas = datas.slice(0, datas.lastIndexOf('&'))
  }
  // 根据打开方式分为 跳转非tab页，关闭当前页跳转非tab页，关闭所有页跳转非tab页，跳转tab页
  switch (parseInt(closeSelf)) {
    case 0:
      uni.navigateTo({
        url: !datas ? path : `${path}?${datas}`
      })
      break;
    case 1:
      uni.redirectTo({
        url: !datas ? path : `${path}?${datas}`
      })
      break;
    case 2:
      uni.reLaunch({
        url: !datas ? path : `${path}?${datas}`
      })
      break;
    case 3:
      uni.switchTab({
        url: !datas ? path : `${path}?${datas}`
      });
      break;
  }
}

// 关闭页面
function close(num = 1) {
  uni.navigateBack({
    delta: num
  })
}

// 弹出一个toast轻提示
function toast(info, isMask = false, position = 'bottom') {
  if (!info) return false
  info = info.toString()
  uni.showToast({
    icon: 'none',
    title: info,
    mask: isMask,
    position
  })
}

// 弹出一个toast图标提示
function showToast(info, iconType = 0, isMask = true, position = 'center') {
  if (!info) return false
  const icons = ['success', 'loading']
  uni.showToast({
    icon: icons[iconType],
    title: info,
    mask: isMask,
    position
  });
}

// 弹出一个loading
function showLoading(title = '加载中', isMask = true) {
  uni.showToast({
    title,
    mask: isMask
  });
}

// 弹出一个模态框
function showModal(content, title = '提示', showCancel = true, confirmText = '确定', cancelText = '取消') {
  if (!content) return false
  return new Promise((resolve, reject) => {
    uni.showModal({
      title,
      content,
      showCancel,
      confirmText,
      cancelText,
      success: function (res) {
        if (res.confirm) {
          resolve(0)
        } else if (res.cancel) {
          resolve(1)
        }
      },
      fail(err) {
        reject(err)
      }
    });
  }).then(res => {
    return [null, res]
  }).catch(err => {
    return [err]
  })
}

// 选择图片-相机/相册
function chooseImgs(count = 1) {
	return new Promise((resolve, reject) => {
		uni.chooseImage({
			count: count, // 相册下的可选图片个数
			sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
			sourceType: ['album', 'camera'], // 从相册选择或相机拍摄
			success(res) {
				resolve(res)
			},
			fail(err) {
				reject(err)
			}
		})
	}).then(res => {
		return [null, res.tempFilePaths]
	}).catch(err => {
		return [err]
	})
}

// 预览图片
function previewImg(obj = {}) {
	let {current, urls, indicator, loop, longPressActions} = obj
	if (!urls) return false
	current = current || 0
	indicator = indicator || 'number'
	loop = loop || false
	longPressActions = longPressActions || {}
	return new Promise((resolve, reject) => {
		uni.previewImage({
			current, // 当前要显示图片的链接/索引值
			urls, // 预览图片的链接列表
			indicator, // "default"底部圆点,"number"顶部数字
			loop, // 是否可循环预览
			longPressActions,  // 长按菜单，默认为保存相册
			success(res) {
				resolve(res)
			},
			fail(err) {
				reject(err)
			}
		})
	}).then(res => {
		return [null, res]
	}).catch(err => {
		return [err]
	})
}

export {
  open, // 页面跳转
  close, // 关闭页面
  toast, // 弹一个toast轻提示
  showToast, // 弹一个toast图标提示
  showLoading, // 弹一个loading
  showModal, // 弹一个模态框
  chooseImgs, // 选择图片
  previewImg, // 预览图片
}